/*
 * FlangeView.java
 */

package flange;

import flange.functions.ProportionFunction;
import flange.functions.Point2D;
import flange.functions.WordsByDayFunction;
import flange.functions.WordsNumberFunction;
import java.awt.image.BufferedImage;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * The application's main frame.
 */
public class FlangeView extends FrameView {

    public FlangeView(SingleFrameApplication app) {
        super(app);

        initComponents();

        Proportionf = new ProportionFunction(new Point2D(0.0,0.0), new Point2D(100.0,50.0));
        WordsNumberf = new WordsNumberFunction(new Point2D(0.0,0.0), new Point2D(100.0,50.0));
        WordsByDayf = new WordsByDayFunction(new Point2D(0.0,0.0), new Point2D(100.0,50.0));
        
        WordsNumber = new Graph2D();
        WordsNumber.setTitle("Количество изученных слов");
        WordsNumber.setLocation(0,0);
        WordsNumber.setSize(600, 200);
        WordsNumberf.k = Proportionf;
        WordsNumber.setFunction(WordsNumberf);
        WordsNumberf.wbdf = WordsByDayf;
        jScrollPanel.add(WordsNumber);

        WordsByDay = new Graph2D();
        WordsByDay.setTitle("Скорость изучения слов");
        WordsByDay.setLocation(0,210);
        WordsByDay.setSize(600, 200);
        WordsByDayf.x = WordsNumberf;
        WordsByDay.setFunction(WordsByDayf);
        jScrollPanel.add(WordsByDay);

        Proportion = new Graph2D();
        Proportion.setTitle("Коэффициент работоспособности");
        Proportion.setLocation(0,420);
        Proportion.setSize(600, 200);
        Proportion.setFunction(Proportionf);
        jScrollPanel.add(Proportion);

    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = FlangeApp.getApplication().getMainFrame();
            aboutBox = new FlangeAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        FlangeApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPaneGraphs = new javax.swing.JScrollPane();
        jScrollPanel = new javax.swing.JPanel();
        SettingsPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabelT = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jSpinnerX0 = new javax.swing.JSpinner();
        jSpinnerXMax = new javax.swing.JSpinner();
        jSliderT = new javax.swing.JSlider();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        EditM = new javax.swing.JTextField();
        EditW = new javax.swing.JTextField();
        EditC = new javax.swing.JTextField();
        EditU = new javax.swing.JTextField();
        EditD = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        saveImageAction = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        jScrollPaneGraphs.setBorder(null);
        jScrollPaneGraphs.setName("jScrollPaneGraphs"); // NOI18N

        jScrollPanel.setAutoscrolls(true);
        jScrollPanel.setMinimumSize(new java.awt.Dimension(600, 100));
        jScrollPanel.setName("jScrollPanel"); // NOI18N
        jScrollPanel.setPreferredSize(new java.awt.Dimension(600, 368));

        javax.swing.GroupLayout jScrollPanelLayout = new javax.swing.GroupLayout(jScrollPanel);
        jScrollPanel.setLayout(jScrollPanelLayout);
        jScrollPanelLayout.setHorizontalGroup(
            jScrollPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 605, Short.MAX_VALUE)
        );
        jScrollPanelLayout.setVerticalGroup(
            jScrollPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 495, Short.MAX_VALUE)
        );

        jScrollPaneGraphs.setViewportView(jScrollPanel);

        SettingsPanel.setName("SettingsPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(flange.FlangeApp.class).getContext().getResourceMap(FlangeView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabelT.setText(resourceMap.getString("jLabelT.text")); // NOI18N
        jLabelT.setName("jLabelT"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(flange.FlangeApp.class).getContext().getActionMap(FlangeView.class, this);
        jButton1.setAction(actionMap.get("RefreshGraphs")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jSpinnerX0.setName("jSpinnerX0"); // NOI18N
        jSpinnerX0.setValue((Integer)0);
        jSpinnerX0.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jSpinnerX0PropertyChange(evt);
            }
        });

        jSpinnerXMax.setName("jSpinnerXMax"); // NOI18N
        jSpinnerXMax.setValue((Integer)3000);
        jSpinnerXMax.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jSpinnerXMaxPropertyChange(evt);
            }
        });

        jSliderT.setMaximum(300);
        jSliderT.setValue(100);
        jSliderT.setName("jSliderT"); // NOI18N
        jSliderT.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jSliderTPropertyChange(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jButton2.setAction(actionMap.get("resetAxis")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        EditM.setText(resourceMap.getString("EditM.text")); // NOI18N
        EditM.setName("EditM"); // NOI18N

        EditW.setText(resourceMap.getString("EditW.text")); // NOI18N
        EditW.setName("EditW"); // NOI18N

        EditC.setEditable(false);
        EditC.setText(resourceMap.getString("EditC.text")); // NOI18N
        EditC.setName("EditC"); // NOI18N

        EditU.setText(resourceMap.getString("EditU.text")); // NOI18N
        EditU.setName("EditU"); // NOI18N

        EditD.setText(resourceMap.getString("EditD.text")); // NOI18N
        EditD.setName("EditD"); // NOI18N

        jButton3.setAction(actionMap.get("setAxis")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        javax.swing.GroupLayout SettingsPanelLayout = new javax.swing.GroupLayout(SettingsPanel);
        SettingsPanel.setLayout(SettingsPanelLayout);
        SettingsPanelLayout.setHorizontalGroup(
            SettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(SettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(SettingsPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelT))
                    .addComponent(jSliderT, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(jSpinnerXMax, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addComponent(jSpinnerX0, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(EditD, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(EditU, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(EditC, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(EditW, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(EditM, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE))
                .addContainerGap())
        );
        SettingsPanelLayout.setVerticalGroup(
            SettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(SettingsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(SettingsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabelT))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSpinnerXMax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSpinnerX0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(EditM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(EditW, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(EditC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(EditU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(EditD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(SettingsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPaneGraphs, javax.swing.GroupLayout.PREFERRED_SIZE, 605, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(127, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(SettingsPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPaneGraphs, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE))
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        saveImageAction.setAction(actionMap.get("saveImage")); // NOI18N
        saveImageAction.setName("saveImageAction"); // NOI18N
        fileMenu.add(saveImageAction);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    private void jSliderTPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jSliderTPropertyChange
        jLabelT.setText(String.valueOf(jSliderT.getValue()));
        WordsNumberf.setDotsX(0, (long)jSliderT.getValue());
        WordsByDayf.setDotsX(0, (long)jSliderT.getValue());
        Proportionf.setDotsX(0, (long)jSliderT.getValue());
    }//GEN-LAST:event_jSliderTPropertyChange

    private void jSpinnerXMaxPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jSpinnerXMaxPropertyChange
        WordsNumberf.xmax = (Integer)jSpinnerXMax.getValue();
    }//GEN-LAST:event_jSpinnerXMaxPropertyChange

    private void jSpinnerX0PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jSpinnerX0PropertyChange
        WordsNumberf.x0 = (Integer)jSpinnerX0.getValue();
    }//GEN-LAST:event_jSpinnerX0PropertyChange

    @Action
    public void RefreshGraphs() {
        Proportionf.m = Double.valueOf(EditM.getText());
        Proportionf.w = Double.valueOf(EditW.getText());
        WordsByDayf.c = Double.valueOf(EditC.getText());
        Proportionf.u = Double.valueOf(EditU.getText());
        WordsNumberf.d = Double.valueOf(EditD.getText());        
        jScrollPanel.repaint();
        setAxis();        
    }

    @Action
    public void resetAxis() {
        WordsNumberf.resetYbyMinMax();
        WordsByDayf.resetYbyMinMax();
        Proportionf.resetYbyMinMax();
    }

    @Action
    public void setAxis() {
        WordsNumberf.updateYbyMinMax();
        WordsByDayf.updateYbyMinMax();
        Proportionf.updateYbyMinMax();
    }

    @Action
    public void saveImage() {
        JFileChooser jf = new JFileChooser();
        if (jf.showSaveDialog(FlangeApp.getApplication().getMainFrame()) == 0) {
        }


    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField EditC;
    private javax.swing.JTextField EditD;
    private javax.swing.JTextField EditM;
    private javax.swing.JTextField EditU;
    private javax.swing.JTextField EditW;
    private javax.swing.JPanel SettingsPanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabelT;
    private javax.swing.JScrollPane jScrollPaneGraphs;
    private javax.swing.JPanel jScrollPanel;
    private javax.swing.JSlider jSliderT;
    private javax.swing.JSpinner jSpinnerX0;
    private javax.swing.JSpinner jSpinnerXMax;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem saveImageAction;
    // End of variables declaration//GEN-END:variables

    private Graph2D WordsNumber, WordsByDay, Proportion;
    private JDialog aboutBox;
    private WordsNumberFunction WordsNumberf;
    private WordsByDayFunction WordsByDayf;
    private ProportionFunction Proportionf;
}
